
import asyncio
from asyncio import Queue,QueueFull,QueueEmpty
import common

class MsgQueue:
    # 达到 _max_queue_size 的时候信箱就满了
    _max_queue_size = 1000

    def __init__(self):
        self.max_size = self._max_queue_size
        self.q = Queue(maxsize=self.max_size)
        pass

    def append(self,item,no_wait=True)->bool:
        if no_wait:
            try:
                self.q.put_nowait(item)
                return True
            except QueueFull as e:
                common.print_exception(e)
                return False
        else:
            self.q.put(item)
            return True

    def pop(self,no_wait=True):
        if no_wait:
            try:
                return self.q.get_nowait()
            except QueueEmpty as e:
                common.print_exception(e)
                return None
        else:
            return self.q.get()
        

    